package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ArrangeProgressBean;
import com.wnhz.work.yz.bean.CarBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zxb
 * @date 2016/12/19
 * @describe 司机信息
 */
public class DriverMessageActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_pick_driver;
    private TextView tv_pick_car;
    private TextView tv_pick_car_time;
    private TextView tv_pick_car_getTime;


    private static final int CHOOSE_DRIVER = 311;
    private static final int CHOOSE_CARD = 312;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_message);
        initData();
        initTitle();
        initView();
    }

    private ArrangeProgressBean.DriverInfoEntity driver_info = new ArrangeProgressBean.DriverInfoEntity();

    private void initData() {
        driver_info = ArrangeRouteActivity.maxBean.getDriver_info();
    }


    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("司机信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setText("保存");
        tv_right.setTextColor(Color.parseColor("#008c99"));
        tv_right.setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);

    }

    private void initView() {
        tv_pick_driver = (TextView) findViewById(R.id.tv_pick_driver);
        tv_pick_car = (TextView) findViewById(R.id.tv_pick_car);
        tv_pick_car_time = (TextView) findViewById(R.id.tv_pick_car_time);
        tv_pick_car_getTime = (TextView) findViewById(R.id.tv_pick_car_getTime);

//        findViewById(R.id.rl_pick_driver).setOnClickListener(this);
//        findViewById(R.id.rl_pick_car).setOnClickListener(this);
//        findViewById(R.id.rl_pickCar_time).setOnClickListener(this);
//        findViewById(R.id.rl_pickCar_getTime).setOnClickListener(this);
        if (!"".equals(driver_info.getCar_name())) {
            tv_pick_car_time.setText(driver_info.getTime());
            tv_pick_car.setText(driver_info.getCar_name());
            tv_pick_driver.setText(driver_info.getDriver_name());
        }
        if (ArrangeRouteActivity.maxBean.getCanUpdata().equals("0")) {

        } else {
            findViewById(R.id.rl_pick_driver).setOnClickListener(this);
            findViewById(R.id.rl_pick_car).setOnClickListener(this);
            findViewById(R.id.rl_pickCar_time).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {

            case R.id.btn_main_left:

                finish();
                break;
            //保存
            case R.id.submit:
                // MyToast("保存");

                String tv_pick_drivers = tv_pick_driver.getText().toString().trim();
                String tv_pick_cars = tv_pick_car.getText().toString().trim();
                String tv_pick_car_times = tv_pick_car_time.getText().toString().trim();
                if ("".equals(tv_pick_drivers)) {
                    MyToast("请选择司机");
                    return;
                }
                if ("".equals(tv_pick_cars)) {
                    MyToast("选择车辆");
                    return;
                }
                if ("".equals(tv_pick_car_times)) {
                    MyToast("请选择接站时间");
                    return;
                }
                driver_info.setCar_name(tv_pick_cars);
                driver_info.setDriver_name(tv_pick_drivers);
                driver_info.setTime(tv_pick_car_times);
                setResult(RESULT_OK);
                finish();

                break;

            //选择司机
            case R.id.rl_pick_driver:
                intent = new Intent(this, DriverPickActivity.class);
                startActivityForResult(intent, CHOOSE_DRIVER);
                break;

            //选择车辆
            case R.id.rl_pick_car:
                intent = new Intent(this, AllCarActivity.class);
                startActivityForResult(intent, CHOOSE_CARD);
                break;

            //接站时间
            case R.id.rl_pickCar_time:
                // MyToast("接站时间");


//                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
//                    @Override
//                    public void onTimeSelect(Date date, View v) {//选中事件回调
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String defaultTime = format.format(date);
//                        tv_pick_car_time.setText(defaultTime);
//                    }
//                })   //.setTitleSize(16)//标题文字大小
//                        // .setTitleText("预算系数")
//                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
//                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
//                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
//                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
//
//                        .setType(TimePickerView.Type.ALL)//默认全部显示
//                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                        .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
//                        .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
//                        .setLabel("年", "月", "日", "时", "分", "秒")
//                        .isDialog(true)//是否显示为对话框样式
//                        .gravity(Gravity.BOTTOM)
//                        .build();
//                pvTime.show();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);
                String[] b = str.split("-");
                Calendar startDate = Calendar.getInstance();
                startDate.set(Integer.parseInt(b[0]), Integer.parseInt(b[1]) - 1, Integer.parseInt(b[2]));
                Calendar endDate = Calendar.getInstance();
                endDate.set(2110, 10, 29);
                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String defaultTime = format.format(date);
                        tv_pick_car_time.setText(defaultTime);
                    }
                })
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)//默认全部显示
                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                        .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
                        .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                        .setRangDate(startDate, endDate)//默认是1960-2110年
                        .setDate(startDate)// 默认是系统时间*/
                        .build();
                pvTime.show();
                break;

//            //预计到达时间
//            case R.id.rl_pickCar_getTime:
//                MyToast("预计到达时间");
//                break;

            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case CHOOSE_DRIVER:
                if (resultCode == RESULT_OK) {
                    if (null != data) {
                        String getDriver = data.getStringExtra("driver");
                        tv_pick_driver.setText(getDriver);
                        driver_info.setDriver_id(data.getStringExtra("driverId"));
                        tv_pick_driver.setText(getDriver);
                    }
                }
                break;
            case CHOOSE_CARD:
                if (resultCode == RESULT_OK) {
                    CarBean.InfoEntity info1 = (CarBean.InfoEntity) data.getSerializableExtra("bean");
                    driver_info.setCar_name(info1.getCar_name());
                    driver_info.setCar_id(info1.getId());
                    tv_pick_car.setText(info1.getCar_name());
                }
            default:
                break;
        }
    }
}
